package com.liser.print.bluetooth.receiver;

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.liser.print.bluetooth.BluetoothClientFactory;


/**
 * 蓝牙监听广播接收器
 *
 * @author Administrator
 * @date 2019/12/30 14:11
 */
public class BluReceiver extends BroadcastReceiver {

    private static final String TAG = BluReceiver.class.getName();

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action != null) {
            switch (action) {
                case BluetoothDevice.ACTION_ACL_CONNECTED:
                    Log.i(TAG, "蓝牙设备已连接");
                    break;
                case BluetoothDevice.ACTION_ACL_DISCONNECTED:
                    Log.i(TAG, "蓝牙设备已断开！尝试重连...");
                    boolean reconnect = BluetoothClientFactory.getClassicBluetoothClient().reconnect();
                    if (reconnect) {
                        Log.i(TAG, "蓝牙设备重连成功");
                    } else {
                        Log.i(TAG, "蓝牙设备重连失败");
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
